home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / ust1a.arc / HXD.C < prev    next >
Encoding:
C/C++ Source or Header  |  1985-09-05  |  4.9 KB  |  228 lines

  1. /*+
  2.  * File: hxd
  3.  * Description:
  4.  *  hxd, Hexadecimal dump is similar unix's "od -x"
  5.  *  Displays the contents of a file in hexadecimal.
  6.  *  If a byte is printable, it is also printed.
  7.  *  format of output is :
  8.  *  dec_add:hex_add word word word word word word word word  string  
  9.  *  where:
  10.  *      dec_add == Start address for that line in decimal. 
  11.  *      hex_add == Start address for that line in hex. 
  12.  *      word == Representation of two bytes in hex. Two characters
  13.  *              for each byte. First two characters in "word" represent
  14.  *              the byte that was read sooner.
  15.  *      string == If the character is printable, it is printed.
  16.  *                Otherwise a '.' is printed. '.'is printed as '.'.
  17.  *  For each in put file name of the file is first emited.
  18.  *      file: <i_file>
  19.  *  last line for each input file is length of the file in 
  20.  *      "dec_add:hex_add" format.
  21.  *
  22.  * Usages:
  23.  *  hxd [-o] <o_file> <i_files>
  24.  *
  25.  *
  26.  *  Author: Mohsen Banan.
  27.  *
  28.  *  This program is public domain software, no warranty intended or
  29.  *  implied.
  30.  *  General permission to copy or modify, but not for profit,  is
  31.  *  hereby  granted.
  32.  *
  33.  *
  34.  * Functions:
  35.  *
  36.  *
  37.  * Audit Trail:  $Log:    hxd.c,v $
  38.  * Revision 1.1  85/08/28  08:38:36  mohsen
  39.  * original posting to the net
  40.  * 
  41.  * 
  42.  *
  43. -*/
  44.  
  45. #ifdef RCS_VER
  46. static char *rcs = "$Header: hxd.c,v 1.1 85/08/28 08:38:36 mohsen Exp $";
  47. #endif
  48.  
  49. /* #includes */
  50. #include "mbstd.h"
  51. #include <stdio.h>
  52. #include <ctype.h>
  53. #include <fcntl.h>
  54. #ifdef unix
  55. #include "msc3.h"
  56. #endif
  57.  
  58. /* #defines */
  59.  
  60. /* external variables */
  61.  
  62. /* referenced external function declarations */
  63.  
  64. /* internal function declarations */
  65. PUBLIC VOID hxd();
  66. PUBLIC VOID hxdump();
  67. PUBLIC VOID cant_open();
  68. PUBLIC VOID printisc();
  69. STATIC VOID usage();
  70.  
  71. /* global variables */
  72.  
  73. /* static variables */
  74. STATIC CHAR * prog_name;
  75.  
  76. INT
  77. main (argc, argv)
  78. INT argc;
  79. CHAR * argv[];
  80. {
  81.     hxd(argc, argv);
  82. }
  83.     
  84.  
  85. /*<
  86.  * Function:hxd
  87.  * Description:
  88.  *  Parses the command line and calls hxdump.
  89.  *  
  90.  * Returns:
  91.  *  VOID
  92.  * 
  93. >*/
  94. PUBLIC VOID 
  95. hxd (argc,argv)
  96. INT argc;
  97. CHAR * argv[];
  98. {
  99.     FILE * i_fp;
  100.     FILE * o_fp;
  101.     INT i;
  102.  
  103.     prog_name = argv[0];
  104.     i_fp = stdin;
  105.     o_fp = stdout;
  106.     for (i=1; i<argc; ++i) {
  107.         if (*argv[i] == '-') {
  108.             /* To handle concatenated switches */
  109.             INT j;
  110.             j=i;
  111.             while (*(++argv[j])) {
  112.                 switch (*argv[j]) {
  113.                 case 'o':
  114.                 case 'O':
  115.                     if ( !(o_fp = fopen(argv[++i], "w")) ) {
  116.                         cant_open(prog_name, argv[i]);
  117.                         exit(1);
  118.                     }
  119.                     break;
  120.                 default:
  121.                     usage();
  122.                     exit(1);
  123.                 } /* switch (*argv[j]) */
  124.             } /* while (*(++argv[j])) */
  125.         } /* if '-' */
  126.         else {
  127.             if (!(i_fp = fopen (argv[i], "r"))) {
  128.                 cant_open (prog_name,  argv[i]);
  129.                 exit (1);
  130.             } 
  131.             fprintf (o_fp, "file:   %s", argv[i]);
  132.             setmode (fileno(i_fp), O_BINARY);
  133.             setmode (fileno(o_fp), O_TEXT);
  134.             hxdump (i_fp, o_fp);
  135.             fclose (i_fp);
  136.         }
  137.     } /* for i<argc */
  138.     fclose (o_fp);
  139.     exit (0);
  140. }
  141.  
  142.  
  143. /*<
  144.  * Function:HX_DUMP
  145.  * Description:
  146.  *
  147.  * Arguments:
  148.  *
  149.  * Returns:
  150.  *
  151.  * Side Effects:
  152.  *
  153.  * Calls:
  154.  * 
  155. >*/
  156. PUBLIC VOID
  157. hxdump (i_fp, o_fp)
  158. FILE * i_fp;
  159. FILE * o_fp;
  160. {
  161. #define BYTESPERLINE 16
  162.     LONG i = 0L;
  163.     INT c;
  164.     INT chars[BYTESPERLINE];
  165.     INT j=0;
  166.     INT k;
  167.  
  168.     while ((c = getc (i_fp)) != EOF) {
  169.         if (! (i % BYTESPERLINE)) {
  170.             if (i) {
  171.                 j=0;
  172.                 fprintf (o_fp, "  ");
  173.                 printisc (o_fp,chars, BYTESPERLINE);
  174.             }
  175.                 
  176.             fprintf (o_fp, "\n%07ld:%07lx", i,i );
  177.         }
  178.         if (!(i++ & 1)) {
  179.             putc (' ', o_fp);
  180.         }
  181.         fprintf (o_fp, "%02x", c);
  182.         chars[j++] = c;
  183.     }
  184.     if (k = (i % BYTESPERLINE)) {
  185.         INT jj;
  186.         k = BYTESPERLINE - k;
  187.         for (jj=0; jj < (5*k/2)  ;++jj) {
  188.             putc(' ', o_fp);
  189.         }
  190.     }
  191.     fprintf (o_fp, "  ");
  192.     printisc (o_fp, chars, BYTESPERLINE);
  193.     fprintf (o_fp, "\n%07ld:%07lx\n", i,i);
  194. }
  195.  
  196. PUBLIC VOID
  197. cant_open (prog_name,filename)
  198. CHAR * prog_name;
  199. CHAR * filename;
  200. {
  201.     fprintf (stderr, "%s :can not open %s \n", prog_name, filename);
  202.  
  203. STATIC VOID
  204. usage ()
  205. {
  206.     fprintf (stderr, "Usage: %s [-o] <o_file> <i_file> \n", prog_name);
  207.      
  208. }
  209.  
  210.  
  211. PUBLIC VOID
  212. printisc (o_fp, chars, length)
  213. FILE * o_fp;
  214. INT chars[];
  215. INT length;
  216. {
  217.     INT i;
  218.  
  219.     for (i=0; i<length; ++i) {
  220.         if (isprint (chars[i])) {
  221.             putc (chars[i], o_fp);
  222.         } else {
  223.             putc ('.', o_fp);
  224.         }
  225.     }
  226. }
  227.